﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMSoftware.Models;
using Entities;

namespace RMSoftware.Controllers
{
    public class MeasureController : Controller
    {
        [HttpGet]
        public ActionResult Create(Guid id)
        {
            DataManager dm = new DataManager();

            ViewBag.ProjectTitle = dm.GetRisk(id).Project.Title;
            ViewBag.RiskTitle = dm.GetRisk(id).RiskFactor.Title;
            ViewBag.Persons = dm.Persons;

            return View(new Measure { ProjectRiskId = id });
        }

        [HttpPost]
        public ActionResult Create(Measure obj)
        {
            DataManager dm = new DataManager();

            obj.MeasureId = Guid.NewGuid();
            dm.AddMeasure(obj);
            dm.SendEmailThroughGmail("[Управление угрозами проекта \"" + obj.ProjectsRisk.Project.Title + 
                "\"] - Новое мероприятие", "Вас назначили ответственным за мероприятие \"" +
                obj.Title + "\" по отработке угрозы \"" + obj.ProjectsRisk.RiskFactor.Title + "\".", obj.Person.Email);

            return RedirectToAction("ViewDetails", "Risk", new { id = obj.ProjectRiskId });
        }

        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            DataManager dm = new DataManager();
            Measure m = dm.GetMeasure(id);
            dm.DeleteMeasure(m);

            return RedirectToAction("ViewDetails", "Risk", new { id = m.ProjectRiskId });
        }

        public ActionResult Learn(Guid id)
        {
            DataManager dm = new DataManager();
            Measure measure = dm.GetMeasure(id);

            measure.BegDate = DateTime.Now;
            dm.SaveMeasure(measure);

            return RedirectToAction("ViewDetails", "Person", new { personId = measure.PersonId });
        }

        [HttpGet]
        public ActionResult UploadReport(Guid id)
        {
            ViewData.Model = (new DataManager()).GetMeasure(id);

            return View();
        }

        [HttpPost]
        public ActionResult UploadReport(HttpPostedFileBase reportFile, Guid measureId)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "UploadedFiles/";
            string fileExtension = System.IO.Path.GetExtension(reportFile.FileName);
            string reportFilename = Guid.NewGuid() + fileExtension;
            reportFile.SaveAs(System.IO.Path.Combine(path, reportFilename));

            DataManager dm = new DataManager();
            Measure measure = dm.GetMeasure(measureId);

            measure.EndDate = DateTime.Now; // при загрузке отчета устанавливаем дату окончания мероприятия
            measure.ReportFileName = reportFilename; // и сохраняем id файла отчета
            dm.SaveMeasure(measure);
            dm.SendEmailThroughGmail("[Управление угрозами проекта \"" + measure.ProjectsRisk.Project.Title +
                "\"] - Завершение мероприятия", "Мероприятие \"" + measure.Title + "\" по отработке угрозы \"" + 
                measure.ProjectsRisk.RiskFactor.Title + "\" завершено.", measure.ProjectsRisk.Project.Officer.Email);

            return RedirectToAction("ViewDetails", "Person", new { personId = measure.PersonId });
        }

        public ActionResult DownloadReport(Guid measureId)
        {
            DataManager dm = new DataManager();

            Measure measure = dm.GetMeasure(measureId);
            string path = AppDomain.CurrentDomain.BaseDirectory + "UploadedFiles/";
            string filename = measure.ReportFileName;

            return File(System.IO.Path.Combine(path, filename), "application/" + System.IO.Path.GetExtension(filename), Server.UrlEncode(filename));
        }
    }
}
